var t = require("../../@babel/runtime/helpers/interopRequireDefault").default, e = require("../../config/CT_share_index/index.js"), n = t(require("../../utils/Tips")), a = t(require("../../kta/index")), o = t(require("../../api/api_CTShare")), i = 0, s = getApp();

a.default.Page({
    data: {
        unionid: "",
        poster: "",
        windowWidth: "",
        windowHeight: "",
        btn_save_img: e.imageBaseUrl.CT_share_index + "btn_save.png",
        bgImg: e.imageBaseUrl.CT_share_index + "bg_face.png",
        bgImg_local: "",
        qrcode: "",
        qrcodeFromInterNet: ""
    },
    onLoad: function(t) {
        this.init();
    },
    init: function() {
        var t = this;
        s.globalData.aucMiniAuth.sid ? t.getWindowWidth().then(function() {
            return t.getQrImgUrl();
        }).then(function(e) {
            return Promise.all([ t.getBgTempImage(), t.getQRTempImage(e) ]);
        }).then(function() {
            return t.drawImage();
        }).catch(function() {
            wx.hideLoading();
        }) : (n.default.toast("缺少sid,重新获取"), t.getMiniAuth().then(function() {
            return t.getWindowWidth();
        }).then(function() {
            return t.getQrImgUrl();
        }).then(function(e) {
            return Promise.all([ t.getBgTempImage(), t.getQRTempImage(e) ]);
        }).then(function() {
            return t.drawImage();
        }).catch(function() {
            wx.hideLoading();
        }));
    },
    getMiniAuth: function() {
        return new Promise(function(t, e) {
            o.default.getInviteOrder().then(function() {
                t();
            }).catch(function() {
                e();
            });
        });
    },
    getWindowWidth: function() {
        wx.showLoading({
            title: "加载中"
        });
        var t = this;
        return new Promise(function(e, a) {
            wx.getSystemInfo({
                success: function(n) {
                    t.setData({
                        windowWidth: n.windowWidth,
                        windowHeight: n.windowHeight
                    }), e();
                },
                fail: function() {
                    n.default.toast("获取系统信息失败"), a();
                }
            });
        });
    },
    getQrImgUrl: function() {
        return new Promise(function(t, e) {
            var a = "";
            s.globalData.aucMiniAuth.sid ? a = "?page=".concat(encodeURI("pages/CT_share_newUser/index"), "&sid=").concat(s.globalData.aucMiniAuth.sid, "&channel=").concat(s.globalData.aucChannel) : (n.default.toast("缺少分享信息sid"), 
            e());
            var o = s.globalData.baseUrl + "/actops-client/v0/mini/miniPageCode" + a;
            console.log("二维码图片地址", o), t(o);
        });
    },
    getQRTempImage: function(t) {
        var e = this;
        return new Promise(function(a, o) {
            t || (wx.showModal({
                content: "未获得分享二维码，请刷新重试",
                showCancel: !1
            }), o());
            var i = e;
            wx.getImageInfo({
                src: t,
                success: function(e) {
                    console.log("获取二维码图片临时路径成功", e), e.path && e.width > 0 && e.height > 0 ? (i.setData({
                        qrcodeFromInterNet: t,
                        qrcode: e.path
                    }), a(e.path)) : (wx.showModal({
                        content: "获取二维码图片临时路径失败，尺寸或路径不对",
                        showCancel: !1
                    }), o());
                },
                fail: function(t) {
                    n.default.toast("获取二维码图片临时路径失败"), console.log("获取二维码图片临时路径失败", t), o(t);
                }
            });
        });
    },
    getBgTempImage: function() {
        var t = this;
        return new Promise(function(e, a) {
            var o = t;
            wx.getImageInfo({
                src: o.data.bgImg,
                success: function(t) {
                    console.log("获取背景图片临时路径成功", t), t.path && t.width > 0 && t.height > 0 ? (o.setData({
                        bgImg_local: t.path
                    }), e()) : (wx.showModal({
                        content: "获取背景图片临时路径失败，尺寸或路径不对",
                        showCancel: !1
                    }), a());
                },
                fail: function(t) {
                    n.default.toast("获取背景图片临时路径失败"), console.log("获取背景图片临时路径失败", t), a();
                }
            });
        });
    },
    drawImage: function(t) {
        var e = this;
        return new Promise(function(t, n) {
            e.data.bgImg_local && e.data.qrcode || (wx.showModal({
                content: "未获取到背景图或二维码，请重新进入页面",
                showCancel: !1
            }), wx.hideLoading(), n()), wx.showLoading({
                title: "加载中"
            }), console.log("drawImage");
            var a = e, o = wx.createCanvasContext("canvasPoster", e), s = e.data.windowWidth, c = 1.82 * e.data.windowWidth, r = .38 * s, u = .5 * s - .5 * r, d = .66 * s;
            o.drawImage(e.data.bgImg_local, 0, 0, s, c), o.save(), o.drawImage(e.data.qrcode, u, d, r, .96 * r), 
            o.save(), o.draw(), setTimeout(function() {
                wx.canvasToTempFilePath({
                    canvasId: "canvasPoster",
                    x: 0,
                    y: 0,
                    destWidth: 2 * s,
                    destHeight: 2 * c,
                    success: function(e) {
                        console.log("canvas合成图片地址成功", e), a.setData({
                            poster: e.tempFilePath
                        }), t();
                    },
                    fail: function(t) {
                        console.log("canvas合成图片地址失败", t), a.setData({
                            poster: ""
                        }), (i += 1) < 3 ? a.drawImage() : wx.showModal({
                            content: t.errMsg,
                            showCancel: !1
                        }), n();
                    },
                    complete: function() {
                        wx.hideLoading();
                    }
                }, a);
            }, 200);
        });
    },
    savePoster: function() {
        a.default.clickStat("c5b5146b5e684658942a8cee5d8ed0f8", {
            userphone: s.globalData.aucPhone
        });
        var t = this;
        this.data.poster ? wx.saveImageToPhotosAlbum({
            filePath: this.data.poster,
            success: function(t) {
                wx.showToast({
                    title: "分享画报已保存，快去分享给好友吧。",
                    icon: "none"
                });
            },
            fail: function() {
                wx.showModal({
                    content: "保存失败，请授权允许使用我的相册",
                    showCancel: !1,
                    success: function() {
                        wx.openSetting({
                            success: function(e) {
                                e.authSetting["scope.writePhotosAlbum"] ? t.savePoster() : wx.showModal({
                                    content: "您没有打开保存相册功能，不能保存图片哦~点击右上角设置允许访问相册",
                                    showCancel: !1
                                });
                            },
                            fail: function() {
                                n.default.toast("打开设置失败");
                            }
                        });
                    }
                });
            }
        }) : n.default.toast("无图片，请截图保存");
    }
});